package cn.bqjr.eily.samples.controller.common;

import cn.bqjr.eily.mybatis.spring.boot.utils.PagingUtils;
import cn.bqjr.eily.samples.constants.SysConstant;
import cn.bqjr.eily.samples.controller.base.BaseController;
import cn.bqjr.eily.samples.entity.po.Menu;
import cn.bqjr.eily.samples.facade.IMenuFacade;
import cn.bqjr.eily.samples.service.base.IMenuService;
import cn.bqjr.eily.web.modules.apiversion.ApiVersion;
import cn.bqjr.eily.web.utils.AssertUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("api/{version}/menus")
@ApiVersion(1)
public class MenuController extends BaseController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IMenuFacade menuFacade;

    /**
     * 新增菜单
     * @param menu
     * @return
     */
    @PostMapping
    public Object addMenu(@RequestBody Menu menu) {
        return menuService.insert(menu);
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @DeleteMapping(value = "/{menuId}")
    public Object deleteMenu(@PathVariable("menuId") Long menuId) {
        return menuFacade.deleteMenu(menuId);
    }

    /**
     * 批量删除菜单
     * @param menuIds
     * @return
     */
    @DeleteMapping
    public Object deleteMenuByBatchId(@RequestParam("ids") List<Long> menuIds) {
        return menuFacade.deleteMenuByBatchId(menuIds);
    }

    /**
     * 更新菜单
     * @param menuId
     * @param menu
     * @return
     */
    @PutMapping(value = "/{menuId}")
    public Object updateMenu(@PathVariable("menuId") Long menuId,
                             @RequestBody Menu menu) {
        AssertUtils.equals(menuId, menu.getMenuId());
        return menuService.updateById(menu);
    }

    /**
     * 查询指定菜单信息
     * @param menuId
     * @return
     */
    @GetMapping(value = "/{menuId}")
    public Object selectMenu(@PathVariable("menuId") Long menuId) {
        return menuService.selectById(menuId);
    }

    /**
     * 查询菜单列表
     * @param parentMenuId
     * @return
     */
    @GetMapping
    public Object selectMenuList(
            @RequestParam(value = "parentMenuId", required = false,
                    defaultValue = "" + SysConstant.DEFAULT_PARENT_MENU_ID) long parentMenuId) {
        PagingUtils.pagingIfExist(request);
        return menuService.selectList(
                new EntityWrapper<Menu>().eq(Menu.PARENTMENUID, parentMenuId)
                                         .orderBy(Menu.SORTNO));
    }

    /**
     * 查询菜单树
     * @param parentMenuId
     * @return
     */
    @GetMapping(params = "tree=true")
    public Object selectMenuTree(
            @RequestParam(value = "parentMenuId", required = false,
                    defaultValue = "" + SysConstant.DEFAULT_PARENT_MENU_ID) long parentMenuId) {
        PagingUtils.clearPageInfo(request);
        return menuService.fillMenuTree(
                (List<Menu>) selectMenuList(parentMenuId),
                Arrays.asList(SysConstant.ADMIN_ROLE_ID),
                true);
    }
}
